package api

import (
	"api-getway/basic/config"
	__ "api-getway/basic/proto"
	"api-getway/handler/req"
	"github.com/gin-gonic/gin"
	"net/http"
)

func VideoAdd(c *gin.Context) {
	var so req.VideoAddRequest
	err := c.ShouldBind(&so)
	if err != nil {
		c.JSON(http.StatusBadRequest, &ResPonce{
			Code: 400,
			Msg:  "参数错误",
			Data: err.Error(),
		})
		return
	}

	r, err := config.VideoClient.VideoAdd(c, &__.VideoAddReq{
		Title:  so.Title,
		Intro:  so.Intro,
		Image:  so.Image,
		Cation: so.Cation,
		Region: so.Region,
		Year:   so.Year,
	})

	if err != nil {
		c.JSON(http.StatusBadRequest, &ResPonce{
			Code: 400,
			Msg:  "视频添加失败",
			Data: err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, &ResPonce{
		Code: 200,
		Msg:  "视频添加成功",
		Data: r,
	})
}

func VideoShow(c *gin.Context) {
	var so req.VideoShow
	err := c.ShouldBind(&so)
	if err != nil {
		c.JSON(http.StatusBadRequest, &ResPonce{
			Code: 400,
			Msg:  "参数错误",
			Data: err.Error(),
		})
		return
	}

	r, err := config.VideoClient.VideoShow(c, &__.VideoShowReq{VideoId: so.VideoId})

	if err != nil {
		c.JSON(http.StatusBadRequest, &ResPonce{
			Code: 400,
			Msg:  "视频列表查询失败",
			Data: err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, &ResPonce{
		Code: 200,
		Msg:  "视频列表查询成功",
		Data: r,
	})
}

func History(c *gin.Context) {
	var so req.History
	err := c.ShouldBind(&so)
	if err != nil {
		c.JSON(http.StatusBadRequest, &ResPonce{
			Code: 400,
			Msg:  "参数错误",
			Data: err.Error(),
		})
		return
	}

	r, err := config.VideoClient.History(c, &__.HistoryReq{
		UserId:    so.UserId,
		VideoId:   so.VideoId,
		EpisodeId: so.EpisodeId,
		Progress:  so.Progress,
		WatchTime: so.WatchTime,
	})

	if err != nil {
		c.JSON(http.StatusBadRequest, &ResPonce{
			Code: 400,
			Msg:  "历史存储失败",
			Data: err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, &ResPonce{
		Code: 200,
		Msg:  "历史记录存储成功",
		Data: r,
	})
}

func Episode(c *gin.Context) {
	var so req.Episode
	err := c.ShouldBind(&so)
	if err != nil {
		c.JSON(http.StatusBadRequest, &ResPonce{
			Code: 400,
			Msg:  "参数错误",
			Data: err.Error(),
		})
		return
	}

	r, err := config.VideoClient.Episode(c, &__.EpisodeReq{
		EpisodeId: so.EpisodeId,
	})

	if err != nil {
		c.JSON(http.StatusBadRequest, &ResPonce{
			Code: 400,
			Msg:  "剧集列表查询失败",
			Data: err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, &ResPonce{
		Code: 200,
		Msg:  "剧集列表查询成功",
		Data: r,
	})
}

func HistoryList(c *gin.Context) {
	var so req.HistoryList
	err := c.ShouldBind(&so)
	if err != nil {
		c.JSON(http.StatusBadRequest, &ResPonce{
			Code: 400,
			Msg:  "参数错误",
			Data: err.Error(),
		})
		return
	}

	r, err := config.VideoClient.HistoryList(c, &__.HistoryListReq{
		HistoryId: so.HistoryId,
	})

	if err != nil {
		c.JSON(http.StatusBadRequest, &ResPonce{
			Code: 400,
			Msg:  "历史列表查询失败",
			Data: err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, &ResPonce{
		Code: 200,
		Msg:  "历史列表查询成功",
		Data: r,
	})
}
